package design.simdi.code.final_paper.site.application;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Request;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.spring.injection.annot.SpringBean;

import design.simdi.code.final_paper.core.daos.UserDao;
import design.simdi.code.final_paper.core.entities.Role;
import design.simdi.code.final_paper.core.entities.User;
import design.simdi.code.final_paper.core.enums.UserStatus;
import design.simdi.code.final_paper.core.services.UserService;
import design.simdi.code.final_paper.utils.CryptoUtils;

public class MyAuthenticatedWebSession extends AuthenticatedWebSession {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SpringBean
	private UserService userService;
	@SpringBean
	private UserDao userDao;
	private User currentUser;

	public MyAuthenticatedWebSession(Request request) {
		super(request);
		InjectorHolder.getInjector().inject(this);
		// TODO
		// daca se doreste crearea de utilizatori impliciti --- decomenteaza
		// aici
		// if (userService.findUserByLogin("admin") == null) {
		// initDataBase();
		// }

	}

	public void initDataBase() {

		userService.initDataBase();

	}

	@Override
	public boolean authenticate(String username, String password) {
		currentUser = getUserService().findUserByNameAndPassword(username, CryptoUtils.getHash(password));
		// daca e logat pentru prima data .. pun contul pe activated.. doar daca
		// statusul era nefolosit
		if (currentUser != null) {
			switch (currentUser.getStatus()) {
			case ACTIVE:
				return true;

			case NEVER_USED:
				userService.changeStatus(currentUser, UserStatus.ACTIVE);
				currentUser = userDao.get(currentUser.getId());
				return true;
			case BLOCKED:
			case REQUEST_ACTIVATE:
			case MARK_FOR_DELETE:
				currentUser = null;
				return false;
			}
		}
		if (currentUser == null)
			return false;
		return true;
	}

	@Override
	public Roles getRoles() {

		if (isSignedIn()) {
			currentUser = userService.findById(currentUser.getId());
			List<Role> listaRoluri = new ArrayList<Role>(currentUser.getRoles());
			String[] listaRoluriString = new String[listaRoluri.size()];
			int i = 0;
			for (Role r : listaRoluri)
				listaRoluriString[i++] = r.getName();

			return new Roles(listaRoluriString);
		}
		return null;
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param currentUser
	 *            the currentUser to set
	 */
	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}

	/**
	 * @return the currentUser
	 */
	public User getCurrentUser() {
		return currentUser;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

}
